package com.atguigu.dga.dolphinscheduler.service.impl;

import com.atguigu.dga.dolphinscheduler.bean.TDsTaskInstance;
import com.atguigu.dga.dolphinscheduler.mapper.TDsTaskInstanceMapper;
import com.atguigu.dga.dolphinscheduler.service.TDsTaskInstanceService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tubage
 * @since 2024-03-19
 */
@Service
@DS("dolphinscheduler")
public class TDsTaskInstanceServiceImpl extends ServiceImpl<TDsTaskInstanceMapper, TDsTaskInstance> implements TDsTaskInstanceService {
    // TODO 获取DS任务实例 Join（mybatis） inSql  exists方法都可(MP)
    @Override
    public List<TDsTaskInstance> getTdsTaskInstanceListByJoin(String assessDate) {
        return getBaseMapper().selectTdsTaskIntanceList(assessDate);
    }

    @Override
    public List<TDsTaskInstance> getTdsTaskInstanceListByIn(String assessDate) {
        return list(
            new QueryWrapper<TDsTaskInstance>()
                    .inSql("id", "select\n" +
                            "        max(id) max_id\n" +
                            "    from t_ds_task_instance\n" +
                            "    where state = 7 and date_format(start_time, '%Y-%m-%d') = '2023-05-02'\n" +
                            "    group by name")

//            .exists("select 1 from\n" +
//                    "    (select\n" +
//                    "         max(id) max_id\n" +
//                    "     from t_ds_task_instance\n" +
//                    "     where state = 7 and date_format(start_time, '%Y-%m-%d') = '2023-05-02'\n" +
//                    "     group by name) t1\n" +
//                    "    where t_ds_task_instance.id = t1.max_id")
        );
    }

    @Override
    public List<TDsTaskInstance> getTdsTaskInstanceListByExists(String assessDate) {
        return list(
                new QueryWrapper<TDsTaskInstance>()
                        .exists("select 1 from\n" +
                                "    (select\n" +
                                "         max(id) max_id\n" +
                                "     from t_ds_task_instance\n" +
                                "     where state = 7 and date_format(start_time, '%Y-%m-%d') = '2023-05-02'\n" +
                                "     group by name) t1\n" +
                                "    where t_ds_task_instance.id = t1.max_id")
        );
    }

    // TODO DS是否有报错指标 返回失败的实例集合
    @Override
    public List<TDsTaskInstance> getFailTdsTaskInstanceList(String name, String assessDate) {
        return list(
                new QueryWrapper<TDsTaskInstance>()
                        .eq("state", 6)
                        .eq("name", name)
                        .eq("DATE_FORMAT(start_time , '%Y-%m-%d')", assessDate)
        );
    }

    // TODO 产出时效指标 返回当前表前days天的实例集合
    @Override
    public List<TDsTaskInstance> getBeforeDaysInstanceList(String name, String assessDateStr, String beforeDaysDateStr) {
        return list(
                new QueryWrapper<TDsTaskInstance>()
                        .exists("select 1 from (\n" +
                                "            select\n" +
                                "                name,\n" +
                                "                date_format(start_time, '%Y-%m-%d'),\n" +
                                "                max(id) max_id\n" +
                                "            from t_ds_task_instance\n" +
                                "            where state = 7\n" +
                                "              and date_format(start_time, '%Y-%m-%d') >= '"+ beforeDaysDateStr +"'\n" +
                                "              and date_format(start_time, '%Y-%m-%d') < '"+ assessDateStr +"'\n" +
                                "              and name = '"+ name +"'\n" +
                                "            group by name, date_format(start_time, '%Y-%m-%d')\n" +
                                "        ) t1\n" +
                                "   where t_ds_task_instance.id = t1.max_id")
        );

    }
}
